package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyParkUserAddBo;
import com.ruoyi.system.bo.ZsyyParkUserQueryBo;
import com.ruoyi.system.bo.ZsyyParkUserEditBo;
import com.ruoyi.system.domain.ZsyyParkUser;
import com.ruoyi.system.mapper.ZsyyParkUserMapper;
import com.ruoyi.system.vo.ZsyyParkUserVo;
import com.ruoyi.system.service.IZsyyParkUserService;
import com.ruoyi.system.mapper.ZsyyParkUserMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 停车场用户Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyParkUserServiceImpl extends ServicePlusImpl<ZsyyParkUserMapper, ZsyyParkUser> implements IZsyyParkUserService {
    @Autowired
    private ZsyyParkUserMapper iZsyyParkUserMapper;

    @Override
    public ZsyyParkUserVo queryById(Long id){
        return getVoById(id, ZsyyParkUserVo.class);
    }

    @Override
    public TableDataInfo<ZsyyParkUserVo> queryPageList(ZsyyParkUserQueryBo bo) {
        PagePlus<ZsyyParkUser, ZsyyParkUserVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyParkUserVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyParkUserVo> queryList(ZsyyParkUserQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyParkUserVo.class);
    }

    private LambdaQueryWrapper<ZsyyParkUser> buildQueryWrapper(ZsyyParkUserQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyParkUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getUserName()), ZsyyParkUser::getUserName, bo.getUserName());
        lqw.eq(StrUtil.isNotBlank(bo.getParkCode()), ZsyyParkUser::getParkCode, bo.getParkCode());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), ZsyyParkUser::getPhone, bo.getPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getCarNo()), ZsyyParkUser::getCarNo, bo.getCarNo());
        lqw.eq(bo.getType() != null, ZsyyParkUser::getType, bo.getType());
        lqw.eq(StrUtil.isNotBlank(bo.getCardNo()), ZsyyParkUser::getCardNo, bo.getCardNo());
        lqw.eq(bo.getLastParkTime() != null, ZsyyParkUser::getLastParkTime, bo.getLastParkTime());
        lqw.eq(bo.getMonthId() != null, ZsyyParkUser::getMonthId, bo.getMonthId());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyParkUserAddBo bo) {
        ZsyyParkUser add = BeanUtil.toBean(bo, ZsyyParkUser.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyParkUserEditBo bo) {
        ZsyyParkUser update = BeanUtil.toBean(bo, ZsyyParkUser.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyParkUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
